#ifndef USER_GlOBAL_H
#define USER_GlOBAL_H
//user_global defines ()
#include "gd32f4xx.h"
#include<math.h>

#include "user_config.h"

//user_def
#define XDATA __attribute__((section("XRAM")))
#define xdata __attribute__((section("XRAM")))
#define IDATA __attribute__((section("DTCM")))
#define XDATA __attribute__((section("XRAM")))

#ifndef   bit
  #define bit  unsigned char
#endif
#ifndef   uchar
  #define uchar  unsigned char
#endif
#ifndef   ushort
  #define ushort unsigned short
#endif
#ifndef   ulong
  #define ulong unsigned long
#endif
#ifndef   uint
  #define uint unsigned long
#endif


#ifndef   u8
  #define u8  unsigned char
  #define U8  unsigned char
#endif
#ifndef   u16
  #define u16 unsigned short
  #define U16 unsigned short
#endif
#ifndef   u32
  #define u32 unsigned long
  #define U32 unsigned long
#endif


//#define UNUSED(x)        ((x)=(x))
#ifndef _STR
#define __STR(x)        #x
#define _STR(x)          __STR(x)
#endif

#ifndef assert
#define assert(expr)
#endif

typedef union tagUU16
{
    U8 UU8[2];
    U16 UU16;
} UU16;


#define USE_BY_USART1
//------------------------------------------------------------------------------
// 常量定义
//------------------------------------------------------------------------------

// 系统频率
#define SYSCLK              INTCLK

//#define UNUSED(x)    ((x)=(x))

#define max(a, b)    ((a) > (b) ? (a) : (b))
#define min(a, b)    ((a) < (b) ? (a) : (b))

#define TIMER_STOPED   0xFFFFFFFF
//------------------------------------------------------------------------------
// 数据类型
//------------------------------------------------------------------------------

typedef ushort Route;

// 端口输出值
typedef struct CS_GPORT
{
  uint32_t port;            // 端口
  uint16_t pin;             // 端口引脚值
} CS_GPORT;

extern CS_GPORT hxclk_gport[1];
extern CS_GPORT hxdat_gport[1];
extern CS_GPORT spics_gport[4];


//将多字节数转换为小端模式
extern ushort from_little_endian_16(ushort n);
//将多字节数转换为小端模式
extern ulong from_little_endian_32(ulong n);

#define MSB        1
#define LSB        0

typedef struct FILE_DESC
{
  ushort mask;
  ushort count;
  union
  {
    ushort *w;
    float *f;
  } ptr;
} FILE_DESC;

enum MACHINE_ROLE{
    S1 = 0,             // 杯盘电机
    S2,                 // 左试剂抽屉电机
    S3,                 // 右试剂抽屉电机
    S4,                 // 上盖电机

    MACHINE_COUNT
};

enum ACTION_PENDING_ROLE
{
    ACTION_PENDING_MODBUS_EVENT = 0,
    ACTION_PENDING_KEY1_EVENT,
    ACTION_PENDING_KEY2_EVENT,
    ACTION_PENDING_KEY3_EVENT,
    ACTION_PENDING_KEY4_EVENT,
};

__packed typedef struct MACINE_INFO
{
    ushort duration;
    long distance;
} MACHINE_INFO;

__packed typedef struct MACINE_PARAM
{
    ushort max_speed;
    long max_distance;
} MACHINE_PARAM;


////////////////////////////////////////////////////////////////////////////////
//
typedef struct READY_SEN
{
    bit state:1;
    bit active:1;
}REAYD_SEN;

typedef struct FIX_VALUE
{
    uchar index;
    uchar enable;            //
    uchar state;              //
    uchar exist;            // 检测试管是否存在电平值
    ushort step;
} FIX_VALUE;

typedef struct FIX_FILE
{
   ushort read;
   ushort write;
   struct FIX_VALUE value[1024];
} FIX_FILE;

extern FIX_FILE fix_file;

enum {
    KEY_STARTS1 = 0,
    KEY_STARTS2,
    KEY_STARTS3,
    KEY_STARTS4,
    KEY_STOP,               // 停止按钮，预留
    KEY_START_COUNT = KEY_STARTS4 + 1
};

enum MACHINE_STATE{
    ME_STATE_IDLE = 0,              // 空闲状态
    ME_STATE_WAIT,                  // 处于等待状态
    ME_STATE_WORKING,               // 处于工作状态，工作状态下检测到可运行，启动动作
    ME_STATE_RETRY,
    ME_STATE_DONE,
};

enum Event{
    EVENT_NONE = 0,
    EVENT_FINISHED
};

void Process_Starter(uchar index);
void Process_Event(uchar index);
void Key_Led_Ctrl(uchar n);


__packed typedef struct LED_CNTRL
{
    ushort led_blink_times;             // 闪烁次数
    ushort led_on_duration;             // 亮时间，ms
    ushort led_off_duration;            // 灭时间，ms
} LED_CNTRL;

#endif // CONFIG_H
